using System;

namespace RenameX.Rules
{
    public class DateTimeRule : RuleBase
    {
        public override string Name => "时间戳";

        public enum DateTimeFormat
        {
            [System.ComponentModel.Description("yyyy-MM-dd HH-mm-ss")]
            DateTime,
            [System.ComponentModel.Description("yyyy-MM-dd")]
            DateOnly,
            [System.ComponentModel.Description("HH-mm-ss")]
            TimeOnly,
            [System.ComponentModel.Description("yyyyMMddHHmmss")]
            Compact,
            [System.ComponentModel.Description("yyyy年MM月dd日")]
            ChineseDate,
            [System.ComponentModel.Description("自定义")]
            Custom
        }

        private DateTimeFormat _format = DateTimeFormat.DateTime;
        public DateTimeFormat Format 
        { 
            get => _format; 
            set => SetProperty(ref _format, value); 
        }

        private string _customFormat = "yyyy-MM-dd_HH-mm-ss";
        public string CustomFormat 
        { 
            get => _customFormat; 
            set => SetProperty(ref _customFormat, value); 
        }

        public enum InsertPosition { Prefix, Suffix, Replace }
        private InsertPosition _position = InsertPosition.Suffix;
        public InsertPosition Position 
        { 
            get => _position; 
            set => SetProperty(ref _position, value); 
        }

        private string _separator = "_";
        public string Separator 
        { 
            get => _separator; 
            set => SetProperty(ref _separator, value); 
        }

        public override string Apply(string originalName)
        {
            var now = DateTime.Now;
            string timeString;

            switch (Format)
            {
                case DateTimeFormat.DateTime:
                    timeString = now.ToString("yyyy-MM-dd HH-mm-ss");
                    break;
                case DateTimeFormat.DateOnly:
                    timeString = now.ToString("yyyy-MM-dd");
                    break;
                case DateTimeFormat.TimeOnly:
                    timeString = now.ToString("HH-mm-ss");
                    break;
                case DateTimeFormat.Compact:
                    timeString = now.ToString("yyyyMMddHHmmss");
                    break;
                case DateTimeFormat.ChineseDate:
                    timeString = now.ToString("yyyy年MM月dd日");
                    break;
                case DateTimeFormat.Custom:
                    try
                    {
                        timeString = now.ToString(CustomFormat);
                    }
                    catch
                    {
                        timeString = now.ToString("yyyy-MM-dd_HH-mm-ss");
                    }
                    break;
                default:
                    timeString = now.ToString("yyyy-MM-dd_HH-mm-ss");
                    break;
            }

            return Position switch
            {
                InsertPosition.Prefix => $"{timeString}{Separator}{originalName}",
                InsertPosition.Suffix => $"{originalName}{Separator}{timeString}",
                InsertPosition.Replace => timeString,
                _ => $"{originalName}{Separator}{timeString}"
            };
        }
    }
}
